import { RepositoryFactory } from '../repositories';

/**
 * Ant Design 核心组件种子数据
 */
export default async function seedAntDesignComponents(repositories: RepositoryFactory): Promise<void> {
  const componentRepo = repositories.getComponentRepository();
  
  console.log('🌱 开始播种 Ant Design 核心组件数据...');

  // 基础组件数据
  const components = [
    {
      component: {
        name: 'Button',
        category: 'basic',
        description: '按钮用于开始一个即时操作',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 100,
        search_keywords: 'button,btn,按钮,点击,操作',
      },
      props: [
        { name: 'type', type: 'string', required: false, description: '设置按钮类型', default_value: 'default', options: ['default', 'primary', 'ghost', 'dashed', 'link', 'text'] },
        { name: 'size', type: 'string', required: false, description: '设置按钮大小', default_value: 'middle', options: ['large', 'middle', 'small'] },
        { name: 'disabled', type: 'boolean', required: false, description: '按钮失效状态', default_value: 'false' },
        { name: 'loading', type: 'boolean', required: false, description: '设置按钮载入状态', default_value: 'false' },
        { name: 'danger', type: 'boolean', required: false, description: '设置危险按钮', default_value: 'false' },
        { name: 'onClick', type: 'function', required: false, description: '点击按钮时的回调' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '最简单的用法',
          code: `import { Button } from 'antd';\n\nexport default () => <Button>按钮</Button>;`,
        },
        {
          title: '按钮类型',
          description: '按钮有多种类型',
          code: `import { Button } from 'antd';\n\nexport default () => (\n  <div>\n    <Button type="primary">Primary</Button>\n    <Button>Default</Button>\n    <Button type="dashed">Dashed</Button>\n    <Button type="text">Text</Button>\n    <Button type="link">Link</Button>\n  </div>\n);`,
        },
      ],
      imports: [
        { import_statement: "import { Button } from 'antd';" },
      ],
    },
    {
      component: {
        name: 'Input',
        category: 'data-entry',
        description: '通过鼠标或键盘输入内容，是最基础的表单域的包装',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 95,
        search_keywords: 'input,输入框,表单,文本框',
      },
      props: [
        { name: 'value', type: 'string', required: false, description: '输入框内容' },
        { name: 'defaultValue', type: 'string', required: false, description: '输入框默认内容' },
        { name: 'placeholder', type: 'string', required: false, description: '输入框占位文本' },
        { name: 'disabled', type: 'boolean', required: false, description: '是否禁用状态', default_value: 'false' },
        { name: 'size', type: 'string', required: false, description: '控件大小', default_value: 'middle', options: ['large', 'middle', 'small'] },
        { name: 'onChange', type: 'function', required: false, description: '输入框内容变化时的回调' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '基本使用',
          code: `import { Input } from 'antd';\n\nexport default () => <Input placeholder="请输入内容" />;`,
        },
      ],
      imports: [
        { import_statement: "import { Input } from 'antd';" },
      ],
    },
    {
      component: {
        name: 'Select',
        category: 'data-entry',
        description: '下拉选择器',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 90,
        search_keywords: 'select,选择器,下拉,dropdown,选择',
      },
      props: [
        { name: 'value', type: 'any', required: false, description: '指定当前选中的条目' },
        { name: 'defaultValue', type: 'any', required: false, description: '指定默认选中的条目' },
        { name: 'placeholder', type: 'string', required: false, description: '选择框默认文字' },
        { name: 'disabled', type: 'boolean', required: false, description: '是否禁用', default_value: 'false' },
        { name: 'allowClear', type: 'boolean', required: false, description: '支持清除', default_value: 'false' },
        { name: 'mode', type: 'string', required: false, description: '设置 Select 的模式', options: ['multiple', 'tags'] },
        { name: 'onChange', type: 'function', required: false, description: '选中 option，或 input 的 value 变化时，调用此函数' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '基本选择器',
          code: `import { Select } from 'antd';\n\nconst { Option } = Select;\n\nexport default () => (\n  <Select defaultValue="lucy" style={{ width: 120 }}>\n    <Option value="jack">Jack</Option>\n    <Option value="lucy">Lucy</Option>\n    <Option value="disabled" disabled>Disabled</Option>\n    <Option value="Yiminghe">yiminghe</Option>\n  </Select>\n);`,
        },
      ],
      imports: [
        { import_statement: "import { Select } from 'antd';" },
      ],
    },
    {
      component: {
        name: 'Table',
        category: 'data-display',
        description: '展示行列数据',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 95,
        search_keywords: 'table,表格,数据,列表,grid',
      },
      props: [
        { name: 'dataSource', type: 'array', required: false, description: '数据数组' },
        { name: 'columns', type: 'array', required: false, description: '表格列的配置描述' },
        { name: 'pagination', type: 'object | boolean', required: false, description: '分页器，参考配置项或 pagination 文档，设为 false 时不展示和进行分页' },
        { name: 'loading', type: 'boolean', required: false, description: '页面是否加载中', default_value: 'false' },
        { name: 'size', type: 'string', required: false, description: '表格大小', default_value: 'default', options: ['default', 'middle', 'small'] },
        { name: 'rowKey', type: 'string | function', required: false, description: '表格行 key 的取值，可以是字符串或一个函数' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '简单的表格，最后一列是各种操作',
          code: `import { Table } from 'antd';\n\nconst columns = [\n  {\n    title: '姓名',\n    dataIndex: 'name',\n    key: 'name',\n  },\n  {\n    title: '年龄',\n    dataIndex: 'age',\n    key: 'age',\n  },\n  {\n    title: '住址',\n    dataIndex: 'address',\n    key: 'address',\n  },\n];\n\nconst data = [\n  {\n    key: '1',\n    name: '胡彦斌',\n    age: 32,\n    address: '西湖区湖底公园1号',\n  },\n];\n\nexport default () => <Table columns={columns} dataSource={data} />;`,
        },
      ],
      imports: [
        { import_statement: "import { Table } from 'antd';" },
      ],
    },
    {
      component: {
        name: 'Form',
        category: 'data-entry',
        description: '高性能订阅式表单，一套绝佳的数据域管理流程',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 98,
        search_keywords: 'form,表单,输入,验证,validation',
      },
      props: [
        { name: 'form', type: 'FormInstance', required: false, description: '经 Form.useForm() 创建的 form 控制实例' },
        { name: 'layout', type: 'string', required: false, description: '表单布局', default_value: 'horizontal', options: ['horizontal', 'vertical', 'inline'] },
        { name: 'initialValues', type: 'object', required: false, description: '表单默认值' },
        { name: 'onFinish', type: 'function', required: false, description: '提交表单且数据验证成功后回调事件' },
        { name: 'onFinishFailed', type: 'function', required: false, description: '提交表单且数据验证失败后回调事件' },
        { name: 'validateTrigger', type: 'string | string[]', required: false, description: '统一设置字段校验规则', default_value: 'onChange' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '基本的表单数据域控制展示',
          code: `import { Button, Form, Input } from 'antd';\n\nconst onFinish = (values) => {\n  console.log('Success:', values);\n};\n\nexport default () => (\n  <Form\n    name="basic"\n    layout="vertical"\n    onFinish={onFinish}\n  >\n    <Form.Item\n      label="用户名"\n      name="username"\n      rules={[{ required: true, message: '请输入用户名!' }]}\n    >\n      <Input />\n    </Form.Item>\n\n    <Form.Item>\n      <Button type="primary" htmlType="submit">\n        提交\n      </Button>\n    </Form.Item>\n  </Form>\n);`,
        },
      ],
      imports: [
        { import_statement: "import { Form } from 'antd';" },
      ],
    },
    {
      component: {
        name: 'Modal',
        category: 'feedback',
        description: '模态对话框',
        version: '5.0',
        framework: 'react' as const,
        popularity_score: 85,
        search_keywords: 'modal,对话框,弹窗,popup,dialog',
      },
      props: [
        { name: 'visible', type: 'boolean', required: false, description: '对话框是否可见', default_value: 'false' },
        { name: 'title', type: 'string | ReactNode', required: false, description: '标题' },
        { name: 'width', type: 'string | number', required: false, description: '宽度', default_value: '520' },
        { name: 'footer', type: 'ReactNode | null', required: false, description: '底部内容' },
        { name: 'onOk', type: 'function', required: false, description: '点击确定回调' },
        { name: 'onCancel', type: 'function', required: false, description: '点击遮罩层或右上角叉或取消按钮的回调' },
        { name: 'confirmLoading', type: 'boolean', required: false, description: '确定按钮 loading', default_value: 'false' },
      ],
      examples: [
        {
          title: '基础用法',
          description: '第一个对话框',
          code: `import React, { useState } from 'react';\nimport { Button, Modal } from 'antd';\n\nexport default () => {\n  const [isModalVisible, setIsModalVisible] = useState(false);\n\n  const showModal = () => {\n    setIsModalVisible(true);\n  };\n\n  const handleOk = () => {\n    setIsModalVisible(false);\n  };\n\n  const handleCancel = () => {\n    setIsModalVisible(false);\n  };\n\n  return (\n    <>\n      <Button type="primary" onClick={showModal}>\n        打开对话框\n      </Button>\n      <Modal title="基础对话框" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>\n        <p>一些内容...</p>\n      </Modal>\n    </>\n  );\n};`,
        },
      ],
      imports: [
        { import_statement: "import { Modal } from 'antd';" },
      ],
    },
  ];

  console.log(`📦 准备播种 ${components.length} 个组件...`);

  // 批量创建组件
  for (let i = 0; i < components.length; i++) {
    const componentData = components[i];
    if (!componentData) {
      console.warn(`⚠️  Skipping undefined component at index ${i}`);
      continue;
    }
    try {
      console.log(`📝 创建组件: ${componentData.component.name} (${i + 1}/${components.length})`);
      
      await componentRepo.createWithRelations(componentData);
      console.log(`✅ 组件 ${componentData.component.name} 创建成功`);
    } catch (error) {
      console.error(`❌ 组件 ${componentData.component.name} 创建失败:`, error);
      throw error;
    }
  }

  console.log('🎉 Ant Design 核心组件数据播种完成！');
}